package com.s.core.root.data;

import com.google.common.collect.Lists;
import com.s.core.root.Ad;
import com.s.core.root.D;
import lombok.Getter;
import lombok.Setter;

import java.util.Arrays;
import java.util.List;

@Getter
public class _PicN extends _BasePic {
    private List<_PointN> pointNList1;
    private List<_PointN> pointNList2;

    @Setter
    private int showIndex;
    private final int n;

    public void nextShowIndex() {
        showIndex++;
        if (2 == showIndex) {
            showIndex = 0;
        }
    }

    public _PicN(_AllData allData, D d, int loadMax, Object o) {
        super(allData, d, loadMax, o);
        this.n = (Integer) o;
    }

    private List<D> skip(List<D> dList, int n, boolean isRemainder) {
        List<D> dNList = Lists.newArrayList();
        for (int i = 0; i < dList.size(); i++) {
            if (i % n == (isRemainder ? n - 1 : 0)) {
                dNList.add(dList.get(i));
            }
        }
        return dNList;
    }

    private List<_PointN> initPointNList(List<D> dList, int n, boolean isRemainder) {
        List<D> dNList = skip(dList, n, isRemainder);
        List<_PointN> pointNList = Lists.newArrayList();
        for (int recentIndex = dNList.size() - 1; recentIndex >= 0; recentIndex--) {
            D d = dNList.get(recentIndex);
            int total = Ad.me().getDatePos1Total().get(d.date);
            pointNList.add(new _PointN(this, d, total, isRemainder ? recentIndex + 1 : recentIndex, n, isRemainder));
        }
        if (isRemainder) {
            List<D> tmpDList = skip(dList, n - 1, false);
            D d = tmpDList.get(0);
            int total = Ad.me().getDatePos1Total().get(d.date);
            pointNList.add(new _PointN(this, d, total, 0, n - 1, isRemainder));
        }
        return pointNList;
    }

    @Override
    public void init(List<D> dList, Object o) {
        int n = (Integer) o;
        pointNList1 = initPointNList(dList, n, true);
        pointNList2 = initPointNList(dList, n, false);
    }

    @Override
    public List<? extends _BasePoint> getPointList() {
        return Arrays.asList(pointNList1, pointNList2).get(showIndex);
    }
}
